package edu.ncsu.contractminer.event.topic;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Map.Entry;

import edu.ncsu.contractminer.utilities.Constants;
import edu.ncsu.contractminer.utilities.Dictionaries;
import edu.ncsu.contractminer.utilities.Stemmer;

public class EventTokenSummarizer {

	public static boolean isPunctuation(String token) {
		String left = token.replaceAll("\\p{Punct}", " ");
		if (left.trim().isEmpty())
			return true;
		else
			return false;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		HashMap<String, Integer> tokenMap = new HashMap<String, Integer>();

		HashSet<String> stopWordSet = Dictionaries.getInstance()
				.getStopWordSet();

		Stemmer stemmer = new Stemmer();
		try {
			FileWriter fw = new FileWriter(Constants.eventStatsFile);
			BufferedWriter bw = new BufferedWriter(fw);

			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			String dataSourceName = "NormDB";
			String dbURL = "jdbc:odbc:" + dataSourceName;
			Connection con = DriverManager.getConnection(dbURL, "", "");
			// try and create a java.sql.Statement so we can run queries
			Statement statement = con.createStatement();

			String query0 = "SELECT ModalVerb, EventCandidate, Subject FROM "
					+ Constants.eventTableName;
			statement.execute(query0);
			ResultSet result0 = statement.getResultSet();
			if (result0 != null) {
				while (result0.next()) {
					String eventCandidate = result0.getString("EventCandidate")
							.toLowerCase();
					String[] tokens = eventCandidate.toLowerCase().substring(1,
							eventCandidate.length() - 1).split(",");
					for (String token : tokens) {
						token = token.trim();
						if (!stopWordSet.contains(token)
								&& !isPunctuation(token)) {
							token = stemmer.stemWord(token);
							if (tokenMap.containsKey(token))
								tokenMap.put(token, tokenMap.get(token) + 1);
							else
								tokenMap.put(token, 1);
						}
					}
				}
			}

			for (Entry<String, Integer> e : tokenMap.entrySet()) {
				fw.write(e.getKey() + "\t" + e.getValue() + "\n");
			}

			bw.flush();
			bw.close();
			fw.close();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
